package com.sskj.gocoin.v.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.component.DaggerUserDataComponent;
import com.sskj.gocoin.p.activity.BindEmailPresenter;
import com.sskj.gocoin.utils.CheckUtil;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.TipUtil;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class BindEmailActivity extends BaseActivity<BindEmailPresenter> {

    @BindView(R2.id.bind_email_tv)
    EditText mBindEmailTv;
    @BindView(R2.id.bind_code_tv)
    EditText mCodeTv;
    @BindView(R2.id.bind_pwd_tv)
    EditText mPwdTv;
    @BindView(R2.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.bind_email_submit)
    Button mBindEmailSubmit;
    @BindView(R2.id.bt_send_email_code)
    Button mSendEmailCode;
    private Disposable timeDis;

    @Inject
    UserViewModel userViewModel;
    UserData userData;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bind_email;
    }

    @Override
    public BindEmailPresenter getPresenter() {
        return new BindEmailPresenter();
    }

    @Override
    protected void onViewBinding() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            }
        });
        mToolBar.setTitle(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity2));
        mToolBar.setLeftButtonOnClickLinster(v -> finish());
        ClickUtil.click(mBindEmailSubmit, () -> {
            String email = mBindEmailTv.getText().toString().trim();
            String code = mCodeTv.getText().toString().trim();
            String tpwd = mPwdTv.getText().toString().trim();
            if (TextUtils.isEmpty(email)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity4));
                return;
            }
            if (!CheckUtil.isEmail(email)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBindEmailActivity202));
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity5));
                return;
            }
            if (TextUtils.isEmpty(tpwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.xmldialog_edit_pay_way193));
                return;
            }
            googleCheck(code, email, tpwd);
        });
        ClickUtil.click(mSendEmailCode, () -> {
            String email = mBindEmailTv.getText().toString().trim();
            if (TextUtils.isEmpty(email)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity4));
                return;
            }
            if (!CheckUtil.isEmail(email)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBindEmailActivity202));
                return;
            }
            mPresenter.sendEmai(email);
        });

//        mBindEmailTv.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (CheckUtil.isEmail(s.toString())) {
//                    mBindEmailSubmit.setEnabled(true);
//                } else {
//                    mBindEmailSubmit.setEnabled(false);
//                }
//            }
//        });
    }


    public void onResponseSuccess() {
        setResult(RESULT_OK, new Intent().putExtra("resultData", mBindEmailTv.getText().toString()));
        finish();
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime()
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        mSendEmailCode.setText(aLong + " s");
                    } else {
                        mSendEmailCode.setText(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity9));
                        mSendEmailCode.setEnabled(true);
                    }
                });
    }

    public void sendCheckCodeFail() {
        mSendEmailCode.setEnabled(true);
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    /**
     * 谷歌验证
     */
    private void googleCheck(String code,String email,String tpwd) {
        if (userData == null) {
            return;
        }
      //  if (userData.getConfig().getGoogle_verify() == 1) {
            if (false) {
            TipUtil.googleCheckTip(this, (pwd) -> {
                mPresenter.requestGoogleCheck(pwd,code, email, tpwd);
            });
        } else {
            //关闭之后不作处理
            mPresenter.requestBindEmail(code, email, tpwd);
        }
    }

    /**
     * 谷歌验证通过
     */
    public void googleCheckSuccess(String code,String email,String tpwd) {
        mPresenter.requestBindEmail(code, email, tpwd);
    }

}
